package net.hserver.admin.system.service;

import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.core.ioc.annotation.Bean;
import cn.hserver.core.ioc.annotation.Component;
import lombok.extern.slf4j.Slf4j;
import net.hserver.admin.system.bean.TableColumns;
import net.hserver.admin.system.bean.TableInfo;
import net.hserver.admin.system.gen.Gen;
import net.hserver.admin.system.mapper.TableMapper;

import java.util.*;


/**
 * 代码生成
 *
 * @since 2022-06-12
 */
@Slf4j
@Component
public class CodeService {

    @Autowired
    private TableMapper mapper;

    public List<TableInfo> selectAll() {
        return mapper.selectTableInfo();
    }

    public Map<String, String> genCode(String tableName) {
        TableInfo tableInfo = selectAll().stream().filter(f -> f.getTableName().equals(tableName)).findFirst().orElse(null);
        if (tableInfo==null){
            return null;
        }
        List<TableColumns> tableColumns = mapper.selectTableColumns(tableName);
        String entityCode = Gen.genEntity(tableInfo, tableColumns);
        String mapperCode = Gen.getMapper(tableInfo);
        String serviceCode = Gen.getService(tableInfo, tableColumns);
        String controllerCode = Gen.getController(tableInfo, tableColumns);
        String typesCode = Gen.genTypes(tableInfo, tableColumns);
        String apiCode = Gen.genApi(tableInfo);
        String pageCode = Gen.genPage(tableInfo,tableColumns);
        Map<String, String> data = new LinkedHashMap<>();
        data.put("entityCode", entityCode);
        data.put("mapperCode", mapperCode);
        data.put("serviceCode", serviceCode);
        data.put("controllerCode", controllerCode);
        data.put("typesCode", typesCode);
        data.put("apiCode", apiCode);
        data.put("pageCode", pageCode);
        return data;
    }

}
